<!DOCTYPE HTML>
<html>

<head>

    <meta charset="utf-8">
    <script type="text/javascript" src="jquery.js"></script>

</head>




<body>


    Local:
    <br>
    <video id="localVideo" autoplay></video>
<br>

    error:
    <div id="ee"></div>


    <br>
    <br>
    <br>
    <button id="qq">clicl me</button>

    <script>
        var isCaller = window.location.href.split('#')[1];

        var socket = new WebSocket("wss://" + window.location.host);

        var PeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
        var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
        var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
        navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;

        var iceServer = {

            iceServers: [{ url: 'stun:stun01.sipphone.com' },
            { url: 'stun:stun.ekiga.net' },
            { url: 'stun:stun.fwdnet.net' },
            { url: 'stun:stun.ideasip.com' },
            { url: 'stun:stun.iptel.org' },
            { url: 'stun:stun.rixtelecom.se' },
            { url: 'stun:stun.schlund.de' },
            { url: 'stun:stun.l.google.com:19302' },
            { url: 'stun:stun1.l.google.com:19302' },
            { url: 'stun:stun2.l.google.com:19302' },
            { url: 'stun:stun3.l.google.com:19302' },
            { url: 'stun:stun4.l.google.com:19302' },
            { url: 'stun:stunserver.org' },
            { url: 'stun:stun.softjoys.com' },
            { url: 'stun:stun.voiparound.com' },
            { url: 'stun:stun.voipbuster.com' },
            { url: 'stun:stun.voipstunt.com' },
            { url: 'stun:stun.voxgratia.org' },
            { url: 'stun:stun.xten.com' },
            {
                url: 'turn:numb.viagenie.ca',
                credential: 'muazkh',
                username: 'webrtc@live.com'
            },
            {
                url: 'turn:192.158.29.39:3478?transport=udp',
                credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
                username: '28224511:1379330808'
            },
            {
                url: 'turn:192.158.29.39:3478?transport=tcp',
                credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
                username: '28224511:1379330808'
            },

            {
                url: "turn:13.250.13.83:3478?transport=udp",
                username: "YzYNCouZM1mhqhmseWk6",
                credential: "YzYNCouZM1mhqhmseWk6"
            }


            ]
        };

        var pc = new PeerConnection(iceServer);
        pc.onicecandidate = function (event) {
            if (event.candidate !== null) {
                socket.send(JSON.stringify({
                    "event": "_ice_candidate",
                    "data": {
                        "candidate": event.candidate
                    }
                }));
            }
        };

        document.getElementById("qq").onclick = function () {
            pc.createOffer((desc) => {
                pc.setLocalDescription(desc);
                socket.send(JSON.stringify({
                    "event": "_offer",
                    "data": {
                        "sdp": desc
                    }
                }));
            },
                (error) => {
                    console.log('Failure callback: ' + error);
                });

        }





        navigator.getUserMedia({
            "audio": false,
            "video": true
        }, function (stream) {
            document.getElementById('localVideo').src = URL.createObjectURL(stream);
            pc.addStream(stream);

        }, function (error) {

            document.getElementById('ee').innerHTML = error.name + "   " + error.message;

        });

        socket.onmessage = function (event) {
            console.log(event.data);

            var json = JSON.parse(event.data)


            if (json.event === "_ice_candidate") {
                pc.addIceCandidate(new IceCandidate(json.data.candidate));
            } else if(json.event === "_answer") {
                pc.setRemoteDescription(new SessionDescription(json.data.sdp));

            }
        };
    </script>
</body>

</html>